package com.example.orderapplication

import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
import com.example.orderapplication.adapter.SearchAdapter
import com.example.orderapplication.bean.CategoryBean
import com.example.orderapplication.bean.DataX
import com.google.gson.Gson


class SearchActivity : AppCompatActivity() {
    private val mEtSearch: EditText by lazy { findViewById(R.id.et_search) }
    private val mLvCommodity: ListView by lazy { findViewById(R.id.lv_commodity) }
    private val mBtSearch: Button by lazy { findViewById(R.id.bt_search) }
    private val mList: MutableList<DataX> = arrayListOf()
    private val TAG = "SearchActivity"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_search)
        val json: String = MainActivity().getJson(this, "category.json")
        val categoryBean = Gson().fromJson(json, CategoryBean::class.java)
        val adapter = SearchAdapter(this, mList)

        mBtSearch.setOnClickListener {
            categoryBean.data?.forEachIndexed { index, data ->
                data.dataList?.forEachIndexed { index, dataX ->
                    if (dataX.title.contains(mEtSearch.text.toString())) {
                        mList.add(dataX)
                        adapter.notifyDataSetChanged()
                    }
                }
            }
        }
        mLvCommodity.adapter = adapter
    }
}